package com.apes.fn.server.todoTask;

import com.alibaba.fastjson.JSONObject;
import com.apes.fn.server.workOrder.model.MaintenanceOrder;
import com.apes.fn.server.workOrder.repository.MaintenanceOrderRepository;
import com.apes.framework.api.SpringManager;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.plugin.event.Listener;
import com.apes.framework.plugin.fsm.FsmFlow;
import com.apes.framework.plugin.fsm.FsmFlowInstance;
import com.apes.framework.plugin.fsm.FsmFlowInstanceRepository;
import com.apes.framework.plugin.fsm.FsmFlowRepository;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Optional;

/**
 * Creator: zouyc
 * CreateDate: 2021-03-26 17:36
 * Description:
 **/
@Component("serviceTodoTask")
public class ServiceTodoTask {

    @Listener(topic = "event:todo.formId.get")
    public String obtainOutAssistFormId(SimpleRequest re){
        JSONObject jo =
                Optional.ofNullable(re.getJO()).orElseThrow(()->new RuntimeException("传参有问题--工作台跳转"));
        if(jo.get("aggregateType") == null || jo.get("aggregateId") == null || jo.get("fsmStateInstanceId") == null) return "";

        String aggregateType = jo.getString("aggregateType");
        String aggregateId = jo.getString("aggregateId");
        String fsmStateInstanceId =jo.getString("fsmStateInstanceId");
        if(aggregateType.equals(MaintenanceOrder.class.getName())){
            FsmFlow fsmFlow = SpringManager.getBean(FsmFlowRepository.class).findOne("000263");
            FsmFlowInstance flowInstance = SpringManager.getBean(FsmFlowInstanceRepository.class).findFirstByFsmFlowAndBusinessKey(fsmFlow,aggregateId);
            if(flowInstance == null) return "";
            if ("外协退回".equals(flowInstance.getCurrentState().getName())){
                return "/form/crm.servicePlanItem";
            }
            MaintenanceOrder maintenanceOrder =
                    Optional.ofNullable(SpringManager.getBean(MaintenanceOrderRepository.class).findOne(aggregateId)).orElseThrow(()->new RuntimeException("单据不存在：aggregateId"));
            return MaintenanceOrder.WORK_ORDER_DISPACH_WAIT.equals(maintenanceOrder.getDispatcheState()) ? "/form/crm.work.dispatchOrder"
                    : MaintenanceOrder.WORK_ORDER_DISPACH_DISPATCH.equals(maintenanceOrder.getDispatcheState()) ?"/form/crm.OutWorkOrder"
                    :"";
        }
        return "";
    }
    @Listener(topic = "event:todo.formId.get")
    public String retailReturnFormId(SimpleRequest re){
        JSONObject jo =
                Optional.ofNullable(re.getJO()).orElseThrow(()->new RuntimeException("传参有问题--工作台跳转"));
        if(jo.get("aggregateType") == null || jo.get("aggregateId") == null || jo.get("fsmStateInstanceId") == null) return "";
        if (!jo.getString("aggregateType").equals("com.apes.fn.scm.retailReturn.RetailReturn"))return "";

        String aggregateId = jo.getString("aggregateId");
        if (aggregateId.startsWith("LT"))return "/form/retail.saleReturnWithOrder";
        if (aggregateId.startsWith("LV"))return "/form/retail.saleReturnNoOrder";
        return "";
    }
}
